<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/4
 * Time: 15:33
 */
namespace Controller;
use Model\IndexModel;
use Shop\Controller;

if(!defined('SHOP')){
    header("Location:/index.php");
}

class MemberController extends TooController{
    public function __construct()
    {
        parent::__construct();

        if(!$_SESSION['Home']['Member']['id']){
            header("Location:/index.php?C=Login&F=login");
        }
    }

    public function index(){
        $this->view->display();
    }


    //我的订单
    public function order_list(){
        //订单基本数据表
        $order=$this->model->pdo_sel("select * from sh_order where m_id = '{$_SESSION['Home']['Member']['id']}' and stat !=3",2);
        //订单详情数据
        foreach($order as &$v){
            $v['array']=$this->model->pdo_sel("select s.*,g.name,g.img,p.gname from sh_order_s as s join sh_goods as g on s.g_id = g.id join sh_goods_g as p on s.gg_id = p.id where s.o_id = '{$v['id']}'",2);
        }
        $this->view->assign('order',$order);
        $this->view->display();
    }

    //订单详情
    public function order_msg(){
        $id=$_GET['id'];
        $order=$this->model->pdo_sel("select * from sh_order where id = '$id'",1);
        //订单详情数据
        $order['array']=$this->model->pdo_sel("select s.*,g.name,g.img,p.gname from sh_order_s as s join sh_goods as g on s.g_id = g.id join sh_goods_g as p on s.gg_id = p.id where s.o_id = '{$order['id']}'",2);
        //地址
        $order['path']=json_decode($order['path'],true);
        $this->view->assign('v',$order);
        $this->view->display();
    }

    //订单操作
    public function order_upd(){
        $id=$_GET['id'];

        if($this->model->pdo_upd("sh_order",array('stat'=>$_GET['stat'])," where id = $id limit 1")){
            $this->success('操作成功！',"Member",'order_list');
        }else{
            $this->success('操作失败！',"Member",'order_list');
        }

    }

    //查看物流
    public function ckwl(){
        $id=$_GET['id'];
        $order=$this->model->pdo_sel("select * from sh_order where id = $id");
        $url="http://www.kuaidi100.com/query?type={$order['wl_n']}&postid={$order['wl_h']}";

        $str=file_get_contents($url); //请求接口
        $arr=json_decode($str,true); //物流数据

        $this->view->assign('arr',$arr);
        $this->view->display();
    }
    public function pass_upd(){
        $this->view->display();
        if(!empty($_POST)) {
            $arr = array();
            $arr['pass'] = $_POST['password'];
            $arr['newpass'] = $_POST['newpassword'];
            $arr['user'] = $_SESSION['Home']['Member']['id'];
//            var_dump($arr);exit;
            if ($arr['pass'] == $arr['newpass']) {
                $arr['pass'] = md5($arr['pass']);
                if ($this->model->pdo_upd('sh_member', array('pass' => $arr['pass']), " where id={$arr['user']}")) {
                   exit("<script>alert('修改成功');
location.href='./index.php?C=Member&F=index';</script>");
                }else{
                    echo "<script>alert('修改失败,密码不能与旧密码相同')</script>";
                }
            } else {
//                $this->retuAjax(array('type'=>2,'msg'=>'密码不一致'));
                echo "<script>alert('修改失败,密码不一致')</script>";
            }
        }
    }

    public function order_path(){

        $arr=$this->model->pdo_sel("select * from sh_path where m_id={$_SESSION['Home']['Member']['id']}",2);

        $this->view->assign('arr',$arr);
        $this->view->display();
    }
    public function path_del(){
        if($this->model->pdo_exec("delete from sh_path where id ={$_GET['id']}")){
            echo "<script>alert('删除成功');location.href='./index.php?C=Member&F=order_path';</script>";
        }else{
            echo "<script>alert('删除失败');location.href='./index.php?C=Member&F=order_path';</script>";
        }
    }

    //地址编辑
    public function path(){

        $arr=$this->model->pdo_sel("select *from sh_path where id={$_GET['id']}");

        if($_POST){
            var_dump($_GET['id']);exit;
            $arr=array();
            $arr['m_id']=$_SESSION['Home']['Member']['id'];
            $arr['sheng']=$_POST['sheng'];
            $arr['shi']=$_POST['shi'];
            $arr['qu']=$_POST['qu'];
            $arr['txt']=$_POST['txt'];
            $arr['ycode']=$_POST['ycode'];
            $arr['name']=$_POST['name'];
            $arr['phone']=$_POST['phone'];
            $this->model->pdo_upd("sh_path",$arr," where id={$_GET['id']}");

            $this->redirect('Member', "order_path");

        }
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //我的收藏
    public function like(){
        $arr=$this->model->pdo_sel("select l.*,g.name,g.img from sh_like as l join sh_goods as g on l.g_id = g.id where m_id = '{$_SESSION['Home']['Member']['id']}'",2);
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

}